package org.eae.eldf.webapp.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.dto.base.PortalCompanyUserBean;
import org.eae.eldf.dto.base.PortalCompanyUserForm;
import org.eae.eldf.dto.base.PortalCompanyUserQueryDto;
import org.eae.eldf.dto.module.ModuleInfoQueryDto;
import org.eae.eldf.entity.base.BaseCompanyInfo;
import org.eae.eldf.entity.company.PortalCompanyUser;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.framework.exception.ServiceException;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.service.base.BaseCompanyInfoService;
import org.eae.eldf.service.base.PortalCompanyUserService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/base")
public class PortalCompanyUserController extends AbstractController<PortalCompanyUserBean>{

	private PortalCompanyUserService portalCompanyUserService;
	
	@Autowired
	public void setPortalCompanyUserService(PortalCompanyUserService portalCompanyUserService) {
		this.portalCompanyUserService = portalCompanyUserService;
	}

	private BaseCompanyInfoService companyInfoService;
	
	@Autowired
	public void setCompanyInfoService(BaseCompanyInfoService companyInfoService) {
		this.companyInfoService = companyInfoService;
	}


	/**
	 * 跳转到list页面
	 * @Date        :      2016-6-7
	 * @param map
	 * @param searchValue
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user")
	public String listPage(ModelMap map,@RequestParam(required=false)String searchValue,HttpServletRequest request,HttpServletResponse response){
		request.setAttribute("allCompanyList", companyInfoService.queryAll());
		return "/base/user/list";
	}
	
	
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/user/list")
	public  @ResponseBody ResultList<PortalCompanyUserBean> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@ModelAttribute(value="userInfoQueryDto") PortalCompanyUserQueryDto userInfoQueryDto ){
		
		Pagination<PortalCompanyUserBean> pager = super.newPaginationInstance(userInfoQueryDto);
		pager = portalCompanyUserService.queryList(pager,userInfoQueryDto);
		return outPageArrayList(pager,userInfoQueryDto);
	}
	
	@RequestMapping(value = "/user/add")
	@ResponseBody
	public Result insert(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "portalCompanyUserForm") PortalCompanyUserForm portalCompanyUserForm) {
		
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try{
			portalCompanyUserService.addUser(portalCompanyUserForm);
		}catch(Exception e){
			result.setResultValue(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage().equals("1001")?"两次密码输入不一致":(e.getMessage().equals("1002")?"用户名已经存在":""));
			}else{
				e.printStackTrace();
				result.setMessage("系统异常");
			}
		}
		
		return result;
	}
}
